const useMidware = require(':utils/useMidware')
const { db, command, cloud } = require(':share/webServer')
const checkJson = require(':utils/checkJson')
const { dbCheck } = require(':utils/cloudCheck')

async function checkData(ctx, next)
{
    const scheam = {
        type: 'object',
        properties: {
            prize_id: { type: 'number' }
        },
        required: ['prize_id']
    }
    checkJson(ctx.request.body, scheam)
    await next()
}

async function deletePrize(ctx, next)
{
    const { prize_id } = ctx.request.body
    let rsp = dbCheck(await db.collection('Prizes').where({
        prize_id
    }).remove(), { noEmpty: true })

    //TODO: 如果prize_id不存在，prize_num还是会被减一

    await db.collection('Prizes_info').where({
        name: 'prize_num'
    }).update({
        value: command.inc(-1)
    })
    await next()
}

useMidware.post('/modules/adminPage/managePrize/deletePrize', checkData)
useMidware.post('/modules/adminPage/managePrize/deletePrize', deletePrize)
